package com.zhangyj.entitis;

import javax.servlet.http.HttpServletRequest;

import com.zhangyj.util.Tools;

public class SystemParam {
	public static final String NAME = "_systemparam";
	private static final String KEY = "_pid";
	int functionId = -1;

	public static SystemParam getSystemParam(HttpServletRequest request) {
		SystemParam param =(SystemParam) request.getAttribute(NAME);
		if (param != null) {
			return param;
		}
		return new SystemParam(request);
	}

	private SystemParam(HttpServletRequest request) {
		String param = Tools.getParameter(request, KEY);
		if (param.length() > 0) {
			functionId = Integer.parseInt(param);
		}
		request.setAttribute(NAME, this);
	}

	public int getFunctionId() {
		return functionId;
	}

	public void setFunctionId(int functionId) {
		this.functionId = functionId;
	}

	public String toUrlParam() {
		if (functionId == -1) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		builder.append(KEY).append("=").append(functionId);
		return builder.toString();
	}

	public String toInput() {
		if (functionId == -1) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		builder.append("<input type=\"hidden\" name=\"").append(KEY).append("\" value=\"").append(functionId).append("\"/>");
		return builder.toString();
	}
}
